<?php
/**
 * 员工
 */
apf_require_controller("HrAbstract");
apf_require_class('Bll_Hr_Property');
apf_require_class('Bll_Hr_User');
apf_require_class('Bll_Hr_UserRelation');
apf_require_class('Bll_Hr_Department');
apf_require_class('Const_Work');
apf_require_class('Const_Util');
apf_require_class('Const_User');
apf_require_class('Bll_Hr_Job');
apf_require_class('Util_BaseUrls');
class Hr_Staff_AddController extends HrAbstractController {

    private $bll_user;
    private $bll_property;
    private $bll_user_relation;
    private $bll_job;
    private $bll_department;
    private $error_item;//验证错误项
    public function handle_request() {
        $params = $this->request->get_parameters();
        $this->bll_user = new Bll_Hr_User();
        $this->bll_job = new Bll_Hr_Job();
        $this->bll_user_relation = new Bll_Hr_UserRelation();
        $this->bll_property = new Bll_Hr_Property();
        $this->bll_department = new Bll_Hr_Department();
        //是否提交正常
        $is_post_ok = false;
        //提交页不需有主管名
        $need_name = true;
        //页面提交
        if($this->is_post_request()){
            ## 验证必填项
            if($this->_check_input_data($params)){
               $is_post_ok = true;
               $need_name = false;
            }
        }

        $user = array();//用户基本信息
        $on_work = array();//任职信息
        $part_time = array();//兼职信息
        $work_derector_direct_yes = array();//任职 直线主管
        $work_derector_direct_no  = array();//任职 虚线主管
        $depart_max_num = 0;//最大兼职id

        if(isset($params['user_id']) && $params['user_id']){//编辑
            $user_id = intval($params['user_id']);

            ##员工基本信息
            $user = $this->bll_user->get_user_by_id($user_id);
            if(empty($user)){
               $this->response->redirect('/');
            }
            ##已签约次数
            if($user['user_type'] == Const_User::USER_TYPE_TRY_STAFF || $user['user_type'] == Const_User::USER_TYPE_FORMAL_STAFF){//适用员工和正式员工
                #/$job_logs = $this->bll_user->get_join_log($user_id);
                #/if(!$job_logs){
                #/    $job_logs = array();
                #/ }
                #/$user['join_num']  =count($job_logs);
                //当前合同也签了
                #/$user['join_num'] = $user['join_num'] + 1;
                ##服务年限
                if($user['enter_date']){
                    $time_length = time() - strtotime($user['enter_date']);
                    if($time_length >0 ){
                        $t = $time_length / (365*24*60*60);
                        $user['work_time'] = round($t, 2);
                    }else{
                        $user['work_time'] =0;
                    }
                }else{
                    $user['work_time'] =0;
                }
            }else{
                #/$user['join_num'] = 0;
                $user['work_time']= 0;
            }

            ##任职信息
            $on_work = $this->bll_user_relation->get_job_by_uids($user_id,Const_Work::WORK_MAIN,Const_Util::IS_DELETE_NO);
            if(!empty($on_work)){
                $on_work = $on_work[0];
                $job = $this->bll_job->find_by_job_id($on_work['job_id']);
                $on_work['job_name'] = $job['position_name'];
                $temp_depart = $this->bll_department->get_department_info_by_id($job['department_id'],Const_Util::IS_DELETE_NO);
                $on_work['job_name'] .= ' - '.$temp_depart['city_name'].$temp_depart['bu_name'].$temp_depart['department_name'];
                $on_work['work_time'] = $on_work['work_time']?date('Y-m-d',strtotime($on_work['work_time'])):'';
                //直线主管
                $work_derector_direct_yes = $this->bll_user_relation->get_director_direct_yes($user_id,$on_work['job_id'],$need_name);
                //虚线主管
                $work_derector_direct_no = $this->bll_user_relation->get_director_direct_no($user_id,$on_work['job_id'],$need_name);
            }

            ##下级人数
            $user['lower_num'] =0;
            $sub_users = $this->bll_user_relation->get_user_sub($user_id, $on_work['job_id'],Const_Work::WORK_MAIN);
            if(!$sub_users){
                $sub_users = array();
            }
            $user['lower_num'] = count($sub_users);

            //兼职信息
            $part_time = $this->bll_user_relation->get_job_by_uids($user_id,Const_Work::WORK_PART_TIME,Const_Util::IS_DELETE_NO);
            if(!empty($part_time)){
                $jobids = array();
                foreach($part_time as $part){
                    $jobids[] = $part['job_id'];
                }
                $temp_jobs = $this->bll_job->find_by_job_ids($jobids,Const_Util::IS_DELETE_NO);
                $jobs = array();
                foreach($temp_jobs as $one){
                    $jobs[$one['job_id']] = $one['position_name'];
                    $temp_depart = $this->bll_department->get_department_info_by_id($one['department_id'],Const_Util::IS_DELETE_NO);
                    $jobs[$one['job_id']] .= ' - '.$temp_depart['city_name'].$temp_depart['bu_name'].$temp_depart['department_name'];
                }
                foreach($part_time as $index => $value){
                    $part_time[$index]['job_name']  = $jobs[$value['job_id']];
                    $part_time[$index]['work_time'] = $value['work_time']?date('Y-m-d',strtotime($value['work_time'])):'';
                    $part_time[$index]['derector_direct_yes'] = $this->bll_user_relation->get_director_direct_yes($user_id,$value['job_id'],$need_name);//直线
                    $part_time[$index]['derector_direct_no'] = $this->bll_user_relation->get_director_direct_no($user_id,$value['job_id'],$need_name);//虚线
                    if($depart_max_num < $value['work_id']){
                        $depart_max_num = $value['work_id'];
                    }
                }
            }
        }else{//添加
            $user = $this->_get_init_user_data();
        }

        if($this->is_post_request() && $is_post_ok){
            ##用户基本数据
            $user_post      = $this->_build_post_user_params($params);
            ##任职信息
            $work_post      = $this->_build_post_work_params($params);
            ##任职主管虚线主管
            $work_relation_post =  $this->_build_post_work_relation_params($params);
            ##兼职信息
            $part_time_post = $this->_build_post_part_time_params($params);
            ##兼职主管虚线主管
            $part_time_relation_post = $this->_build_post_part_time_relation_params($params);
            //save log 20130111,添加修改员工都写log
            $this->bll_user->save_log($params);
            $user_post['last_update'] = time();
            /**  避免自己为主管  避免重复关系 */
            if($user_post['user_id']){//编辑
                unset($user_post['work_city_id']);
                $user_id = $user_post['user_id'];
                $old_user = $this->bll_user->get_user_by_id($user_id);
                ## 1 更新用户信息
                $this->bll_user->update_user($user_id,$user_post);
                ## 2 是否合同续约
                $now = intval(date('Ymd'));
                if($user_post['join_start'] > $old_user['join_start'] && $user_post['join_end'] > $old_user['join_end']){
                    //添加续约记录
                    $this->bll_user->add_join_log($old_user);
                }
                ## 3 任职
                // 3.1 开始担任本职日期
                $this->bll_user_relation->update_user_work($work_post['work_id'], array('work_time'=>$work_post['work_time']));
                // 3.2 直属上级
                if($work_relation_post['parent_id'] != $work_derector_direct_yes['parent_id'] || $work_relation_post['parent_job_id'] != $work_derector_direct_yes['parent_job_id']){
                    //临时删除
                    $this->bll_user_relation->delete_user_report_relation($work_relation_post['zx_relation_id']);
                    //新增
                    $this->bll_user_relation->add_user_report_relation($user_id, $work_post['job_id'], $work_relation_post['parent_id'], $work_relation_post['parent_job_id'], Const_Work::WORK_MAIN);
                }
                // 3.3 虚线主管
                 //先被删除的
                 $old_no_direct_relation_ids = array();
                 //页面点击删除的
                 foreach($work_derector_direct_no as $in=>$direct_no){
                     if(!in_array($direct_no['relation_id'],$work_relation_post['xx_relation_id'])){
                         $this->bll_user_relation->delete_user_report_relation($direct_no['relation_id']);
                     }
                     $old_no_direct_relation_ids[] = $direct_no['relation_id'];
                 }
                 foreach($work_relation_post['no_direct_parent_id'] as $key => $no_direct_parent_id){
                     //新增
                     if(!in_array( $work_relation_post['xx_relation_id'][$key], $old_no_direct_relation_ids)){
                         $this->bll_user_relation->add_user_report_relation($user_id, $work_post['job_id'], $no_direct_parent_id, $work_relation_post['no_direct_parent_job_id'][$key], Const_Work::WORK_PART_TIME);
                     }
                     //修改
                     if( $this->_is_change_no_director($work_relation_post['no_direct_parent_id'][$key], $work_relation_post['no_direct_parent_job_id'][$key],$work_derector_direct_no)){
                         //临时删除
                         $this->bll_user_relation->delete_user_report_relation($work_relation_post['xx_relation_id'][$key]);
                         //新增
                         $this->bll_user_relation->add_user_report_relation($user_id,$work_post['job_id'], $work_relation_post['no_direct_parent_id'][$key], $work_relation_post['no_direct_parent_job_id'][$key], Const_Work::WORK_PART_TIME);
                     }
                 }
                ## 4 兼职
                //$part_time;job_id parent_id
                $old_work_ids = array();
                $old_part_time = array();
                foreach($part_time as $index => $part){
                    $old_work_ids[] = $part['work_id'];
                    $old_part_time[$part['work_id']] = $part;
                }
                $work_ids = array();##先保证删除
                foreach ($part_time_post['work_id'] as $index => $work_id){
                    if(!$part_time_post['job_id'][$index] || !$part_time_post['work_time'][$index]){
                        continue;
                    }
                    $work_ids[] = $work_id;
                }
                // 4.1 页面点击删除的
                foreach ($part_time as $key=>$val){
                    if(!in_array($val['work_id'],$work_ids)){
                        //删除 兼职信息
                        $this->bll_user_relation->update_user_work($val['work_id'], array('is_deleted'=>Const_Util::IS_DELETE_YES));
                        //删除 主管关系
                        if($val['derector_direct_yes']){
                            $this->bll_user_relation->delete_user_report_relation($val['derector_direct_yes']['relation_id']);
                        }
                        //删除 虚线主管关系
                        foreach($val['derector_direct_no'] as $direct_yes){
                            $this->bll_user_relation->delete_user_report_relation($direct_yes['relation_id']);
                        }
                    }
                }
                foreach ($part_time_post['work_id'] as $index => $work_id){
                    if(!$part_time_post['job_id'][$index] || !$part_time_post['work_time'][$index]){
                        continue;
                    }
                    //新增
                    if(!in_array($work_id, $old_work_ids)){
                        $this->_add_part_time_info($user_id,$part_time_post['job_id'][$index],$part_time_post,$index,$part_time_relation_post);
                    }else{
                        //任职有修改 （job变动，工作时间变动）
                        if($part_time_post['job_id'][$index] != $old_part_time[$work_id]['job_id'] || $part_time_post['work_time'][$index] != $old_part_time[$work_id]['work_time']){
                            //临时删除
                            $this->bll_user_relation->update_user_work($work_id, array('is_deleted'=>Const_Util::IS_DELETE_YES));
                            //新增
                            $temp = array();
                            $temp['work_time'] = $part_time_post['work_time'][$index];
                            $temp['job_id']    = $part_time_post['job_id'][$index];
                            $this->bll_user_relation->add_user_work($temp,$user_id,Const_Work::WORK_PART_TIME);
                        }
                        //直属主管关系有修改 （job变动，主管变动，主管工作变动）

                        if($part_time_post['job_id'][$index] != $old_part_time[$work_id]['job_id'] || $part_time_relation_post['parent_id'][$index] != $old_part_time[$work_id]['derector_direct_yes']['parent_id'] || $part_time_relation_post['parent_job_id'][$index] != $old_part_time[$work_id]['derector_direct_yes']['parent_job_id']){
                            //临时删除
                            $this->bll_user_relation->delete_user_report_relation($part_time_relation_post['zx_relation_id'][$index]);
                            //新增
                            $this->bll_user_relation->add_user_report_relation($user_id, $part_time_post['job_id'][$index], $part_time_relation_post['parent_id'][$index], $part_time_relation_post['parent_job_id'][$index], Const_Work::WORK_MAIN);
                        }

                        //虚线主管关系有变动 （job变动，主管变动，主管工作变动）
                        $old_derector_ids = array();
                        $old_derector = array();
                        foreach($old_part_time[$work_id]['derector_direct_no'] as $in=>$direct_no){
                            $old_derector_ids[] = $direct_no['relation_id'];
                        }
                        $relation_ids = array();##先保证删除
                        foreach($part_time_relation_post['no_direct_parent_id'][$index] as $key => $no_direct_parent_id){
                            $relation_ids[] = $part_time_relation_post['xx_relation_id'][$index][$key];
                        }
                        //页面点击删除的
                        foreach($old_part_time[$work_id]['derector_direct_no'] as $in=>$direct_no){
                            if(!in_array($direct_no['relation_id'],$relation_ids)){
                                $this->bll_user_relation->delete_user_report_relation($direct_no['relation_id']);
                            }
                        }
                        foreach($part_time_relation_post['no_direct_parent_id'][$index] as $key => $no_direct_parent_id){
                            //新增
                            if(!in_array( $part_time_relation_post['xx_relation_id'][$index][$key], $old_derector_ids)){
                                $this->bll_user_relation->add_user_report_relation($user_id, $part_time_post['job_id'][$index], $no_direct_parent_id, $part_time_relation_post['no_direct_parent_job_id'][$index][$key], Const_Work::WORK_PART_TIME);
                            }
                            //修改
                            if($part_time_post['job_id'][$index] != $old_part_time[$work_id]['job_id'] || $this->_is_change_no_director($part_time_relation_post['no_direct_parent_id'][$index][$key], $part_time_relation_post['no_direct_parent_job_id'][$index][$key],$old_part_time[$work_id]['derector_direct_no'])){
                                //临时删除
                                $this->bll_user_relation->delete_user_report_relation($part_time_relation_post['xx_relation_id'][$index][$key]);
                                //新增
                                $this->bll_user_relation->add_user_report_relation($user_id, $part_time_post['job_id'][$index], $part_time_relation_post['no_direct_parent_id'][$index][$key], $part_time_relation_post['no_direct_parent_job_id'][$index][$key], Const_Work::WORK_PART_TIME);
                            }
                        }
                    }
                }
            }else{//添加

                ##保存用户基本信息
                $user_id = $this->bll_user->add_user($user_post);

                ##任职
                $this->bll_user_relation->add_user_work($work_post,$user_id,Const_Work::WORK_MAIN);

                ## 任职 主管
                $this->bll_user_relation->add_user_report_relation($user_id, $work_relation_post['job_id'], $work_relation_post['parent_id'], $work_relation_post['parent_job_id'], Const_Work::WORK_MAIN);

                ## 任职 虚线主管
                foreach($work_relation_post['no_direct_parent_id'] as $index => $direct){
                    $this->bll_user_relation->add_user_report_relation($user_id, $work_relation_post['job_id'], $direct, $work_relation_post['no_direct_parent_job_id'][$index], Const_Work::WORK_PART_TIME);
                }

                ##部门变动 更新部门人数
                $this->bll_user_relation->update_dep_position($user_id, 1);//1==增加

                ##兼职
                foreach($part_time_post['job_id'] as $index => $job_id){
                    if(!$job_id || !$part_time_post['work_time'][$index]){
                        continue;
                    }
                    $this->_add_part_time_info($user_id,$job_id,$part_time_post,$index,$part_time_relation_post);
                }
            }
            //成功跳转到列表页（跳转到查看列表页时的状态？？）
            $this->response->redirect('/hr/staffmanage/');
        }else{//post 失败
            //还原提交前状态
            if($this->is_post_request()){
                //用户基本信息
                $user = $this->_build_post_error_user_params($params,$user);
                //任职信息
                $on_work = $this->_build_post_error_on_work_params($params);
                //任职信息  直线主管
                $work_derector_direct_yes = $this->_build_post_error_derector_params($params);
                //任职信息  虚线主管
                $work_derector_direct_no = $this->_build_post_error_no_derector_params($params);
                if($this->error_item == "isIdCardNo"){
                    $user['identity_code'] = '';
                    $user['birthday'] = '';
                }
                //兼职信息
                $part_time = $this->_build_post_error_part_time($params);
            }
        }

        $user = $this->_format_user($user);//格式化数据
        $work_derector_direct_yes = $this->_format_vip_mike($user,$work_derector_direct_yes);
        //用户基本信息
        $this->request->set_attribute('user',$user);
        //任职信息
        $this->request->set_attribute('work',$on_work);
        //兼职信息
        $this->request->set_attribute('part_time',$part_time);
        //任职信息  直线主管
        $this->request->set_attribute('work_derector_direct_yes',$work_derector_direct_yes);
        //任职信息  虚线主管
        $this->request->set_attribute('work_derector_direct_no',$work_derector_direct_no);

        //获取签约公司
        $this->request->set_attribute('agree_company',$this->bll_property->get_agree_company_list());
        //获取合同签约城市
        $this->request->set_attribute('agree_city',$this->_get_all_city());
        //员工类型
        $this->request->set_attribute('user_type',$this->bll_property->get_all_user_type());
        //员工类型
        $this->request->set_attribute('p_degree',$this->bll_property->get_p_degree());
        //员工类型
        $this->request->set_attribute('m_degree',$this->bll_property->get_m_degree());

        //最大兼职id
        $this->request->set_attribute('depart_max_num',$depart_max_num);
        //编辑转岗
        $this->request->set_attribute('staff_transfer_url',Util_BaseUrls::staff_transfer_url(intval($params['user_id'])));

        //下级人数
        $this->request->set_attribute('lower_people_url',Util_BaseUrls::build_search_url('parent_id', $user['user_id']));
        // 错误日志
        $this->request->set_attribute('error',$this->error_item);
        return "Hr_Staff_Add";
    }
    /**
     * 获取权限内城市
     */
    private function _get_all_city(){
        $result = array();
        $citys = $this->bll_property->get_city_list(true);
        $userinfo = $this->get_user_info();
        $city_ids = $userinfo['role_list'][Const_Flow::ROLE_HR];
        if($city_ids){
            foreach($citys as $city){
                if(in_array($city['city_id'], $city_ids)){
                    $result[] = $city;
                }
            }
        }
        return $result;
    }
    /**
     * post 数据验证
     */
    private function _check_input_data($params){
        $boolean = true;
        //必填项验证
        if(!trim($params['username'])){//用户名
            $this->error_item['username'] = '请输入用户名';
            $boolean = false;
        }
        if(!trim($params['isIdCardNo'])){//身份证号
            $this->error_item['isIdCardNo'] = '请输入用户身份证号';
            $boolean = false;
        }
        /* if($this->bll_user->check_idcard_is_used(trim($params['isIdCardNo']),intval($params['unique_type']))){
            $this->error_item = 'isIdCardNo';
            return false;
        } */
        if(!trim($params['birthday'])){//出生日期
            $this->error_item['birthday'] = '请输入用户生日日期';
            $boolean = false;
        }
        if(!intval($params['pack'])){//签约公司
            $this->error_item['pack'] = '请选择合同签约公司';
            $boolean = false;
        }
        if(!intval($params['packcity'])){//签约城市
            $this->error_item['packcity'] = '请选择合同签约城市';
            $boolean = false;
        }
        if(!intval($params['user_type'])){//用户类型
            $this->error_item['user_type'] = '请选择用户类型';
            $boolean = false;
        }
        if(!trim($params['enter_date'])){//入职日期
            $this->error_item['enter_date'] = '请选择入职日期';
            $boolean = false;
        }
        if(!trim($params['join_start'])){//合同开始日期
            $this->error_item['join_start'] = '请选择合同开始日期';
            $boolean = false;
        }
        if(!trim($params['join_end'])){//合同结束日期
            $this->error_item['join_end'] = '请选择合同结束日期';
            $boolean = false;
        }
        if(trim($params['join_start']) && trim($params['join_end'])){
            $s = strtotime(trim($params['join_start']));
            $e = strtotime(trim($params['join_end']));
            if($s >= $e){
                $this->error_item['join_start_end'] = '合同期开始日期应小于结束时间';
                $boolean = false;
            }
        }
        if(trim($params['trial_start']) || trim($params['trial_end'])){
            $s = strtotime(trim($params['trial_start']));
            $e = strtotime(trim($params['trial_end']));
            if(!$s || !$e){
                $this->error_item['trial_start_end'] = '试用期不能只填一个';
                $boolean = false;
            }
            elseif($s >= $e){
                $this->error_item['trial_start_end'] = '试用期开始日期应小于结束时间';
                $boolean = false;
            }
        }
        if(!trim($params['join_keep'])){//合同期限
            $this->error_item['join_start_end'] = '请输入合同期限';
            $boolean = false;
        }
        if(!$params['user_id']){
            if(!intval($params['positionNameID'])){//职务
                $this->error_item['positionNameID'] = '请选择职位名称';
                $boolean = false;
            }
            if(!intval($params['workCity'])){//工作城市
                $this->error_item['workCity'] = '请选择工作城市';
                $boolean = false;
            }
            $user_info = $this->get_user_info();
            if(!in_array(intval($params['workCity']), $user_info['role_list'][Const_Flow::ROLE_HR])){
                $this->error_item['positionNameID'] = '请选择城市管理权限内的职位';
                $boolean = false;
            }
        }
        if(!trim($params['startWorkTime'])){//开始
            $this->error_item['startWorkTime'] = '请选择开始担任本职日期';
            $boolean = false;
        }
        //mike 除外
        if($params['user_code'] !='SHA001'){
            if(!intval($params['directlyLeaderID']) || !intval($params['directlyLeaderJobID'])){//直属上级
                $this->error_item['directlyLeaderID'] = '请选择直属上级';
                $boolean = false;
            }
        }

        return $boolean;
    }
    /**
     * 规范索引
     */
    private function _build_data_key_index($data){
        if(empty($data) || !is_array($data)){
            return $data;
        }
        $result = array();
        $index = 0 ;
        foreach($data as $val){
            $result[$index] = $val;
            $index++;
        }
        return $result;
    }
    /**
     * 判断兼职信息中的虚线主管是否有修改
     * @param int $parent_id
     * @param int $parent_job_id
     * @param array $relations
     * @return boolean
     */
    private function _is_change_no_director($parent_id,$parent_job_id,$relations){
        $result = true;
        foreach($relations as $relation){
            if($parent_id == $relation['parent_id'] && $parent_job_id == $relation['parent_job_id']){
                $result = false;
            }
        }
        return $result;
    }
    /**
     * 添加兼职
     * @param int $user_id
     * @param int $job_id
     * @param array $part_time_post
     * @param int $index
     * @param array $part_time_relation_post|false
     */
    private function _add_part_time_info($user_id,$job_id,$part_time_post,$index,$part_time_relation_post){
        if(!$job_id){
            return false;
        }
        $temp = array();
        //$temp['work_id']   = $part_time_post['work_id'][$index];
        $temp['work_time'] = $part_time_post['work_time'][$index];
        $temp['job_id']    = $job_id;
        $this->bll_user_relation->add_user_work($temp,$user_id,Const_Work::WORK_PART_TIME);

        ## 兼职 主管
        $this->bll_user_relation->add_user_report_relation($user_id, $job_id, $part_time_relation_post['parent_id'][$index], $part_time_relation_post['parent_job_id'][$index], Const_Work::WORK_MAIN);

        ## 兼职 虚线主管
        foreach($part_time_relation_post['no_direct_parent_id'][$index] as $key => $no_direct_parent_id){
            $this->bll_user_relation->add_user_report_relation($user_id, $job_id, $no_direct_parent_id, $part_time_relation_post['no_direct_parent_job_id'][$index][$key], Const_Work::WORK_PART_TIME);
        }
    }
    /**
     * 获取初始化用户数据
     */
    private function _get_init_user_data(){
        return array(
                'sex'             =>1,//性别
                'identity_type'   =>1,//证件类型
                'degree'          =>3,//最高学位
                'marital_status'  =>1,//婚姻
                'fertility_status'=>1,//生育
                'new_train'       =>1,//是否参加培训
                'join_num'        =>0,//合同次数
                'work_time'       =>0,//工作年限
                'lower_num'       =>0,//下级人数
                );
    }
    /**
     * 前台提交数据转换
     */
    private function _build_post_user_params($params){
        return array(
                'user_id'      =>intval($params['user_id']),//用户id
                'real_name'    =>trim($params['username']),//用户名
                'sex'          =>intval($params['sex']),//性别
                'identity_type'=>intval($params['unique_type']),//证件类型
                'identity_code'=>trim($params['isIdCardNo']),//证件号
                'birthday'     =>trim($params['birthday'])?intval(date('Ymd',strtotime($params['birthday']))):0,//生日
                'native_place' =>trim($params['native_place']),//籍贯
                'identity_address'=>trim($params['cordAdd']),//身份证地址
                'present_address'=>trim($params['xianju']),//现居地址
                'degree'         =>intval($params['degree']),//最高学位
                'graduate_time'  =>trim($params['graduate_time'])?intval(date('Ymd',strtotime($params['graduate_time']))):0,//毕业时间
                'graduate_university'=>trim($params['bisch']),//毕业院校
                'profession'     =>trim($params['zhuanye']),//专业
                'marital_status' =>intval($params['marital_status']),//婚姻
                'fertility_status'=>intval($params['fertility_status']),//生育
                'english_name'   =>trim($params['englishname']),//英文名
                'user_name'      =>trim($params['updateName'])?trim($params['updateName']):trim($params['username']),//改名
                'office_phone'   =>trim($params['tel1']),//公司电话
                'office_phone_ex'=>trim($params['tel2']),//分机
                'bank_name'      =>trim($params['bank']),//银行
                'bank_card'      =>trim($params['bank_card']),//银行卡号
                'cellphone'      =>trim($params['phone']),//手机
                'office_mail'    =>trim($params['email']),//邮箱
                'emergency_people'=>trim($params['emergency_people']),//紧急联系人
                'emergency_phone'=>trim($params['isPhone']),//紧急电话
                'company_id'     =>intval($params['pack']),//合同签约公司
                'city_id'        =>intval($params['packcity']),//签约城市
                'user_type'      =>intval($params['user_type']),//员工类型
                'user_code'      =>$params['user_code'],//工号
                'enter_date'     =>trim($params['enter_date'])?intval(date('Ymd',strtotime($params['enter_date']))):0,//入职时间
                'indue_date'     =>trim($params['indue_date'])?intval(date('Ymd',strtotime($params['indue_date']))):0,//转正时间
                'join_start'     =>trim($params['join_start'])?intval(date('Ymd',strtotime($params['join_start']))):0,//合同开始时间
                'join_end'       =>trim($params['join_end'])?intval(date('Ymd',strtotime($params['join_end']))):0,//合同结束时间
                'join_keep'      =>$params['join_keep'],//合同年限
                'trial_start'    =>trim($params['trial_start'])?intval(date('Ymd',strtotime($params['trial_start']))):0,//试用期开始时间
                'trial_end'      =>trim($params['trial_end'])?intval(date('Ymd',strtotime($params['trial_end']))):0,//试用期结束时间
                'trial_keep'     =>intval($params['trial_keep']),//试用期时常
                'work_city_id'   =>intval($params['workCity']),//工作城市
                'new_train'      =>intval($params['isTrain']),//是否参加培训
                'p_level'        =>intval($params['p_level']),//p级
                'm_level'        =>intval($params['m_level']),//m级
                'join_num'       =>intval($params['join_num'])//合同次数
                );

    }
    /**
     * 还原提交错误数据
     * @param unknown_type $params
     * @return number
     */
    private function _build_post_error_user_params($params,$user_old){
        $user = $this->_build_post_user_params($params);
        //$user['join_num']        =isset($user_old['join_num'])?$user_old['join_num']:0;//合同次数
        if(isset($this->error_item['positionNameID'])){
            $user['work_city_name']="";
            $user['work_city_id']="";
        }
        $user['work_time']       =isset($user_old['work_time'])?$user_old['work_time']:0;//工作年限
        $user['lower_num']       =isset($user_old['lower_num'])?$user_old['lower_num']:0;//下级人数
        return $user;
    }
    /**
     * 任职信息
     * @param array $params
     * @return array
     */
    private function _build_post_work_params($params){
        return array(
                'work_id'=>intval($params['work_id']),
                'job_id' =>intval($params['positionNameID']),
                'work_time'=>$params['startWorkTime']?date('Ymd',strtotime($params['startWorkTime'])):date('Ymd'),
                );
    }
    /**
     * 提交出错还原
     * @param unknown_type $params
     * @return multitype:string number
     */
    private function _build_post_error_on_work_params($params){
        if(isset($this->error_item['positionNameID'])){
            $params['positionName']="";
            $params['positionNameID']="";
            $params['positionName_hidden']="";
        }
        return array(
                'job_name'=>isset($params['positionName'])?trim($params['positionName']):trim($params['positionName_hidden']),
                'work_id'=>intval($params['work_id']),
                'job_id' =>intval($params['positionNameID']),
                'work_time'=>$params['startWorkTime']?date('Y-m-d',strtotime($params['startWorkTime'])):'',
        );
    }
    /**
     * 提交出错还原
     * @param unknown_type $params
     * @return multitype:string number
     */
    private function _build_post_error_derector_params($params){
        $index = strpos($params['directlyLeader'],'-');
        return array(
                'parent_id'=>intval($params['directlyLeaderID']),
                'parent_job_id'=>intval($params['directlyLeaderJobID']),
                'parent_name' =>substr($params['directlyLeader'], 0,$index),
                'parent_job_name'=>substr($params['directlyLeader'], $index+1),
        );
    }
    /**
     * 提交出错还原
     * @param unknown_type $params
     */
    private function _build_post_error_no_derector_params($params){
        $result = array();
        foreach ($params['rzxxRelationId'] as $index => $one){
            if(trim($params['noinDirectLeader'][$index])){
                $pos= strpos($params['noinDirectLeader'][$index],'-');
                $result[] = array(
                        'relation_id'=>$one,
                        'parent_id'  =>$params['nodirectlyLeaderID'][$index],
                        'parent_job_id'=>$params['nodirectlyLeaderJobID'][$index],
                        'parent_name' =>substr($params['noinDirectLeader'][$index], 0,$pos),
                        'parent_job_name'=>substr($params['noinDirectLeader'][$index], $pos+1),
                );
            }
        }
        return $result;
    }
    private function _build_post_error_part_time($params){
        $result = array();
        $xxRelationId = $this->_build_data_key_index($params['xxRelationId']);
        $dpInDirectlyLeaderID = $this->_build_data_key_index($params['dpInDirectlyLeaderID']);
        $dpInDirectlyLeaderJobID = $this->_build_data_key_index($params['dpInDirectlyLeaderJobID']);
        $dpInDirectlyLeader = $this->_build_data_key_index($params['dpInDirectlyLeader']);
        foreach($params['ptPositionNameID'] as $index=>$job_id){
            $temp = array(
                    'job_id'=>$job_id,
                    'job_name'=>$params['ptPositionName'][$index],
                    'work_id'=>$params['jzwork_id'][$index],
                    'work_time'=>$params['jzstarttime'][$index]?date('Y-m-d',strtotime($params['jzstarttime'][$index])):'',
                    );
            $derector_direct_yes = array();
            if(trim($params['dpDirectlyLeader'][$index])){
                $pos= strpos($params['dpDirectlyLeader'][$index],'-');
                $derector_direct_yes = array(
                        'relation_id'=>$params['zxRelationId'][$index],
                        'parent_id'=>$params['dpDirectlyLeaderID'][$index],
                        'parent_job_id'=>$params['dpDirectlyLeaderJobID'][$index],
                        'parent_name' =>substr($params['dpDirectlyLeader'][$index], 0,$pos),
                        'parent_job_name'=>substr($params['dpDirectlyLeader'][$index], $pos+1),
                );
            }
            $temp['derector_direct_yes'] = $derector_direct_yes;
            $derector_direct_no = array();
            foreach($xxRelationId as $key=>$relation_id){
                if(trim($dpInDirectlyLeader[$index][$key])){
                    $pos= strpos($dpInDirectlyLeader[$index][$key],'-');
                    $derector_direct_no[] = array(
                            'relation_id'=>$relation_id,
                            'parent_id'  =>$dpInDirectlyLeaderID[$index][$key],
                            'parent_job_id'=>$dpInDirectlyLeaderJobID[$index][$key],
                            'parent_name' =>substr($dpInDirectlyLeader[$index][$key], 0,$pos),
                            'parent_job_name'=>substr($dpInDirectlyLeader[$index][$key], $pos+1),
                    );
                }
            }
            $temp['derector_direct_no'] = $derector_direct_no;
            $result[] = $temp;
        }
        return $result;
    }
    /**
     * 任职主管及虚线主管
     */
    private function _build_post_work_relation_params($params){
        $no_direct_parent_ids = array();
        $no_direct_parent_job_ids = array();
        $xx_relation_ids = array();
        $indexs = array();
        foreach($params['nodirectlyLeaderID'] as $key=> $one){
            $l_id = intval($one);
            if($l_id == intval($params['directlyLeaderID'])){
                $indexs[] = $key;
            }else{
                $no_direct_parent_ids[] = $l_id;
            }
        }
        foreach($params['nodirectlyLeaderJobID'] as $key=>$one){
            if(!in_array($key,$indexs)){
                $no_direct_parent_job_ids[] = intval($one);
            }
        }
        foreach($params['rzxxRelationId'] as $key=>$one){
            if(!in_array($key,$indexs)){
                $xx_relation_ids[] = intval($one);
            }
        }
        return array(
                'job_id' =>intval($params['positionNameID']),
                'parent_id'=>intval($params['directlyLeaderID']),
                'parent_job_id'=>intval($params['directlyLeaderJobID']),
                'zx_relation_id'=>intval($params['rzzxRelationId']),
                'xx_relation_id'=>$xx_relation_ids,
                'no_direct_parent_id'=>$no_direct_parent_ids,
                'no_direct_parent_job_id'=>$no_direct_parent_job_ids
        );
    }
    /**
     * 兼职信息
     * @param array $params
     * @return array
     */
    private function _build_post_part_time_params($params){
        $work_ids  = array();
        $job_ids   = array();
        $work_time = array();

        foreach($params['jzwork_id'] as $one){
            $work_ids[]=intval($one);
        }
        foreach($params['ptPositionNameID'] as $one){
            $job_ids[]=intval($one);
        }
        foreach($params['jzstarttime'] as $one){
            $work_time[]=$one?date('Ymd',strtotime($one)):date('Ymd');
        }
        return array(
                'work_id' => $work_ids,
                'job_id' => $job_ids,
                'work_time' => $work_time,
                );
    }
    /**
     * 兼职信息
     * 主管和虚线主管
     * @param array $params
     * @return array
     */
    private function _build_post_part_time_relation_params($params){
        $job_ids        = array();
        $parent_ids     = array();
        $parent_job_ids = array();
        $no_direct_parent_ids     = array();
        $no_direct_parent_job_ids = array();
        $zx_relation_ids   = array();
        $xx_relation_ids   = array();

        foreach($params['ptPositionNameID'] as $one){
            $job_ids[]=intval($one);
        }
        foreach($params['dpDirectlyLeaderID'] as $one){
            $parent_ids[] = intval($one);
        }
        foreach($params['zxRelationId'] as $one){
            $zx_relation_ids[] = intval($one);
        }
        foreach($params['dpDirectlyLeaderJobID'] as $one){
            $parent_job_ids[] = intval($one);
        }
        $key = 0;
        $indexs = array();
        foreach($params['dpInDirectlyLeaderID'] as $index=>$one){
            $i = 0;
            foreach($one as $o){
                if($parent_ids[$key] == intval($o)){
                    $indexs[$key][] = $i;
                }else{
                    $no_direct_parent_ids[$key][] = intval($o);
                }
                $i++;
            }
            $key++;
        }
        $key = 0;
        foreach($params['dpInDirectlyLeaderJobID'] as $index=>$one){
            $i = 0;
            foreach($one as $o){
                if(!in_array($i,$indexs[$key])){
                    $no_direct_parent_job_ids[$key][] = intval($o);
                }
            }
            $key++;
        }
        $key = 0;
        foreach($params['xxRelationId'] as $index=>$one){
            $i = 0;
            foreach($one as $o){
                if(!in_array($i,$indexs[$key])){
                    $xx_relation_ids[$key][] = intval($o);
                }
            }
            $key++;
        }

        return array(
                'job_id'   => $job_ids,
                'parent_id'=>$parent_ids,
                'parent_job_id'=>$parent_job_ids,
                'zx_relation_id'  =>$zx_relation_ids,
                'xx_relation_id'  =>$xx_relation_ids,
                'no_direct_parent_id'    =>$no_direct_parent_ids,
                'no_direct_parent_job_id'=>$no_direct_parent_job_ids
        );
    }
    /**
     * 格式话日期格式
     * @param unknown_type $user
     * @return string
     */
    private function _format_user($user){

        $user['birthday'] = $user['birthday']?date('Y-m-d',strtotime($user['birthday'])):'';
        $user['graduate_time']= $user['graduate_time']?date('Y-m-d',strtotime($user['graduate_time'])):'';
        $user['join_start']  = $user['join_start']?date('Y-m-d',strtotime($user['join_start'])):'';
        $user['join_end']    = $user['join_end']?date('Y-m-d',strtotime($user['join_end'])):'';
        $user['trial_start'] = $user['trial_start']?date('Y-m-d',strtotime($user['trial_start'])):'';
        $user['trial_end']  = $user['trial_end']?date('Y-m-d',strtotime($user['trial_end'])):'';
        $user['enter_date'] = $user['enter_date']?date('Y-m-d',strtotime($user['enter_date'])):'';
        $user['indue_date'] = $user['indue_date']?date('Y-m-d',strtotime($user['indue_date'])):'';
        $user['trial_keep'] = $user['trial_keep']?$user['trial_keep']:'';

        $citys = $this->bll_property->get_city_list();
        $user['work_city_name'] = $user['work_city_id']?$citys[$user['work_city_id']]['city_name']:'';
        return $user;
    }
    /**
     * mike 主管问题
     */
    private function _format_vip_mike($user,$work_derector_direct_yes){
        if($user['user_code'] == 'SHA001'){
            return array('parent_name'=>'mike','parent_id'=>'parent_id','parent_job_id'=>'parent_job_id','relation_id'=>'relation_id');
        }else{
            return $work_derector_direct_yes;
        }
    }
}